package com.myframe.demo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.Document;

import com.common.pager.PageHelper;
import com.common.pager.PagerVO;
import com.myframe.demo.service.DemoService;

public class DemoServlet extends HttpServlet {
	Logger log = Logger.getLogger(this.getClass());
	DemoService service = new DemoService();
	protected static int DEFAULT_PAGE_SIZE = 20;
	
	HttpServletRequest req;
	HttpServletResponse res;
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String method = req.getParameter("method");
		log.info("method==="+method);
		this.req=req;
		this.res=res;
		if("pagerDemo".equals(method)){
			pagerDemo();
		}else if("treeDemo".equals(method)){
			treeDemo();
		}else if("save".equals(method)){
			save();
		}else if("delete".equals(method)){
			log.info("deleteByIds==="+req.getParameter("ids"));
		}else{
			log.info("other==="+req.getParameter("ids"));
		}
	}
	private void save() throws ServletException, IOException{
		log.info("textField==="+req.getParameter("textField"));
		req.setAttribute("flag", "T");
		req.getRequestDispatcher("demo/demo-extform.jsp").forward(req, res);
	}
	
	private void pagerDemo() throws ServletException, IOException {
		PageHelper pager = new PageHelper(req);
		List list = service.find(pager);
		PagerVO pagerVo = pager.paginate();
		req.setAttribute("list", list);
		req.setAttribute("pager", pagerVo);
		req.getRequestDispatcher("demo/demo-pager.jsp").forward(req, res);
	}

	private void treeDemo() throws ServletException, IOException {
		Document doc = service.getXMLData();   
        String content = doc.asXML();   
        res.setContentType("text/xml; charset=GB18030");//   
        res.getWriter().write(content);   
	}
	
	
}
